package com.hefu.virtuallife.ui.hotel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.base.BaseActivity;
import com.hefu.virtuallife.customview.DrivingRouteOverLay;

import butterknife.BindView;
import butterknife.OnClick;

public class HotelGuideActivity extends BaseActivity implements RouteSearch.OnRouteSearchListener {
    //31.2513320510,121.4537715912
    //31.2310045344,121.4299106598
    //31.2169855401,121.4077663422


    @BindView(R.id.img_return)
    ImageView imgReturn;
    @BindView(R.id.tool_bar)
    Toolbar toolBar;
    @BindView(R.id.textHotel)
    TextView textHotel;
    @BindView(R.id.imgThumb)
    ImageView imgThumb;
    @BindView(R.id.textPercent)
    TextView textPercent;
    @BindView(R.id.textDesc)
    TextView textDesc;
    @BindView(R.id.textAddr)
    TextView textAddr;
    @BindView(R.id.textAppoinment)
    TextView textAppoinment;
    @BindView(R.id.imgDriver)
    ImageView imgDriver;
    @BindView(R.id.textDriver)
    TextView textDriver;
    @BindView(R.id.imgStar01)
    ImageView imgStar01;
    @BindView(R.id.imgStar02)
    ImageView imgStar02;
    @BindView(R.id.imgStar03)
    ImageView imgStar03;
    @BindView(R.id.imgStar04)
    ImageView imgStar04;
    @BindView(R.id.imgStar05)
    ImageView imgStar05;
    @BindView(R.id.textLicense)
    TextView textLicense;
    @BindView(R.id.textCar)
    TextView textCar;
    @BindView(R.id.map)
    MapView mapView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;


    private RouteSearch mRouteSearch;

    private AMap aMap;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_hotel_guide;
    }

    @Override
    protected void initVariables(@Nullable Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        init();
        doSearch();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null) {
            mapView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null) {
            mapView.onPause();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }


    /**
     * 初始化AMap对象
     */
    private void init() {

        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);
        aMap = mapView.getMap();
        initLocation(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation location) {
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
                        latLng, 18, 30, 0)));
                aMap.clear();
                aMap.addMarker(new MarkerOptions().position(latLng)
                        .icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            }
        });
    }


    private void initLocation(AMapLocationListener aMapLocationListener) {
        //初始化client
        AMapLocationClient locationClient = new AMapLocationClient(this.getApplicationContext());
        AMapLocationClientOption locationOption = getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        // 设置定位监听
        locationClient.setLocationListener(aMapLocationListener);
        locationClient.startLocation();
    }


    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }


    public void doSearch() {
        LatLonPoint mStartPoint = new LatLonPoint(31.2513320510, 121.4537715912);
        LatLonPoint mEndPoint = new LatLonPoint(31.2234400000, 121.4453000000);//31.2234400000,121.4453000000
        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(mStartPoint, mEndPoint);

        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_SINGLE_DEFAULT, null, null, "");
        // 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
        mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
    }


    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS && result != null && result.getPaths() != null && result.getPaths().size() > 0) {
            DriveRouteResult mDriveRouteResult = result;
            final DrivePath drivePath = mDriveRouteResult.getPaths().get(0);
            DrivingRouteOverLay drivingRouteOverlay = new DrivingRouteOverLay(
                    this, aMap, drivePath,
                    mDriveRouteResult.getStartPos(),
                    mDriveRouteResult.getTargetPos(), null);
            drivingRouteOverlay.setNodeIconVisibility(true);//设置节点marker是否显示
            drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况，默认true
            drivingRouteOverlay.removeFromMap();
            drivingRouteOverlay.addToMap();
            drivingRouteOverlay.zoomToSpan();
        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }


    @OnClick(R.id.img_return)
    protected void onFinishClick() {
        finish();
    }
}
